/*     */ package com.cr.common.utils;
/*     */ 
/*     */ import java.net.InetAddress;
/*     */ import java.net.UnknownHostException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ 
/*     */ public class IpUtils {
/*     */   public static String getIpAddr(HttpServletRequest request) {
/*  16 */     if (request == null)
/*  18 */       return "unknown"; 
/*  20 */     String ip = request.getHeader("x-forwarded-for");
/*  21 */     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
/*  23 */       ip = request.getHeader("Proxy-Client-IP"); 
/*  25 */     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
/*  27 */       ip = request.getHeader("X-Forwarded-For"); 
/*  29 */     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
/*  31 */       ip = request.getHeader("WL-Proxy-Client-IP"); 
/*  33 */     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
/*  35 */       ip = request.getHeader("X-Real-IP"); 
/*  38 */     if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
/*  40 */       ip = request.getRemoteAddr(); 
/*  43 */     return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
/*     */   }
/*     */   
/*     */   public static boolean internalIp(String ip) {
/*  48 */     byte[] addr = textToNumericFormatV4(ip);
/*  49 */     return (internalIp(addr) || "127.0.0.1".equals(ip));
/*     */   }
/*     */   
/*     */   private static boolean internalIp(byte[] addr) {
/*  54 */     if (MyStringUtils.isNull(addr) || addr.length < 2)
/*  56 */       return true; 
/*  58 */     byte b0 = addr[0];
/*  59 */     byte b1 = addr[1];
/*  61 */     byte SECTION_1 = 10;
/*  63 */     byte SECTION_2 = -84;
/*  64 */     byte SECTION_3 = 16;
/*  65 */     byte SECTION_4 = 31;
/*  67 */     byte SECTION_5 = -64;
/*  68 */     byte SECTION_6 = -88;
/*  69 */     switch (b0) {
/*     */       case 10:
/*  72 */         return true;
/*     */       case -84:
/*  74 */         if (b1 >= 16 && b1 <= 31)
/*  76 */           return true; 
/*     */       case -64:
/*  79 */         switch (b1) {
/*     */           case -88:
/*  82 */             return true;
/*     */         } 
/*     */         break;
/*     */     } 
/*  85 */     return false;
/*     */   }
/*     */   
/*     */   public static byte[] textToNumericFormatV4(String text) {
/*  97 */     if (text.length() == 0)
/*  99 */       return null; 
/* 102 */     byte[] bytes = new byte[4];
/* 103 */     String[] elements = text.split("\\.", -1);
/*     */     try {
/*     */       long l;
/*     */       int i;
/* 108 */       switch (elements.length) {
/*     */         case 1:
/* 111 */           l = Long.parseLong(elements[0]);
/* 112 */           if (l < 0L || l > 4294967295L)
/* 113 */             return null; 
/* 115 */           bytes[0] = (byte)(int)(l >> 24L & 0xFFL);
/* 116 */           bytes[1] = (byte)(int)((l & 0xFFFFFFL) >> 16L & 0xFFL);
/* 117 */           bytes[2] = (byte)(int)((l & 0xFFFFL) >> 8L & 0xFFL);
/* 118 */           bytes[3] = (byte)(int)(l & 0xFFL);
/* 168 */           return bytes;
/*     */         case 2:
/*     */           l = Integer.parseInt(elements[0]);
/*     */           if (l < 0L || l > 255L)
/*     */             return null; 
/*     */           bytes[0] = (byte)(int)(l & 0xFFL);
/*     */           l = Integer.parseInt(elements[1]);
/*     */           if (l < 0L || l > 16777215L)
/*     */             return null; 
/*     */           bytes[1] = (byte)(int)(l >> 16L & 0xFFL);
/*     */           bytes[2] = (byte)(int)((l & 0xFFFFL) >> 8L & 0xFFL);
/*     */           bytes[3] = (byte)(int)(l & 0xFFL);
/* 168 */           return bytes;
/*     */         case 3:
/*     */           for (i = 0; i < 2; i++) {
/*     */             l = Integer.parseInt(elements[i]);
/*     */             if (l < 0L || l > 255L)
/*     */               return null; 
/*     */             bytes[i] = (byte)(int)(l & 0xFFL);
/*     */           } 
/*     */           l = Integer.parseInt(elements[2]);
/*     */           if (l < 0L || l > 65535L)
/*     */             return null; 
/*     */           bytes[2] = (byte)(int)(l >> 8L & 0xFFL);
/*     */           bytes[3] = (byte)(int)(l & 0xFFL);
/* 168 */           return bytes;
/*     */         case 4:
/*     */           for (i = 0; i < 4; i++) {
/*     */             l = Integer.parseInt(elements[i]);
/*     */             if (l < 0L || l > 255L)
/*     */               return null; 
/*     */             bytes[i] = (byte)(int)(l & 0xFFL);
/*     */           } 
/* 168 */           return bytes;
/*     */       } 
/*     */       return null;
/*     */     } catch (NumberFormatException e) {
/*     */       return null;
/*     */     } 
/*     */   }
/*     */   
/*     */   public static String getHostIp() {
/*     */     try {
/* 175 */       return InetAddress.getLocalHost().getHostAddress();
/* 177 */     } catch (UnknownHostException unknownHostException) {
/* 180 */       return "127.0.0.1";
/*     */     } 
/*     */   }
/*     */   
/*     */   public static String getHostName() {
/*     */     try {
/* 187 */       return InetAddress.getLocalHost().getHostName();
/* 189 */     } catch (UnknownHostException unknownHostException) {
/* 192 */       return "未知";
/*     */     } 
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-common-4.7.0.jar!\com\cr\commo\\utils\IpUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */